cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
PROJECT(mycode VERSION 0.1.0)

# version
execute_process(COMMAND git describe --always --tags
                OUTPUT_VARIABLE GIT_BRANCH_ORIGIN)
if("x" STREQUAL "x${GIT_BRANCH_ORIGIN}")
  set(GIT_BRANCH_ORIGIN "v0.0-0-g0000000")
endif()
string(STRIP ${GIT_BRANCH_ORIGIN} GIT_BRANCH)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in
               ${CMAKE_CURRENT_BINARY_DIR}/version.h @ONLY)

include (common)
set(CMAKE_C_STANDARD 11)

add_package("gh:libuv/libuv@1.47.0")
add_package("gh:DaiDevin/zlog#master")
add_package("gh:DaiDevin/libyaml#master")
add_package("gh:ibireme/yyjson#0.8.0")
add_package("gh:jibsen/parg@1.0.3")

find_package(PkgConfig)
pkg_search_module(sqlite3 REQUIRED sqlite3) #仅演示pkgconfig导入用法
#vcpkg安装用法
find_package(eclipse-paho-mqtt-c CONFIG REQUIRED)
find_package(libmodbus CONFIG REQUIRED)

file(GLOB_RECURSE headers CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/src/**.h") 
file(GLOB_RECURSE sources CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/src/**.c")

set(_project hello)
add_executable(${_project} ${sources} ${headers})
target_link_directories(${_project} PUBLIC ${sqlite3_LIBRARY_DIRS})
target_link_libraries(${_project} PUBLIC 
  uv zlog yaml parg modbus
  ${sqlite3_LIBRARIES} 
  eclipse-paho-mqtt-c::paho-mqtt3c-static
  yyjson
)
target_include_directories(${_project} PUBLIC
  ${CMAKE_CURRENT_BINARY_DIR}
  ${sqlite3_LIBRARY_DIRS}
)
target_link_options(${_project} PUBLIC $<$<CONFIG:Release>:-s>) #strip bin

add_custom_command(
  TARGET ${_project}
  POST_BUILD
  COMMAND cp $<TARGET_FILE:${_project}> $<TARGET_FILE:${_project}>_${GIT_BRANCH}
  COMMENT "Add version information to target.")
